package com.example.leetcode;

import static jdk.nashorn.internal.objects.NativeArray.reverse;

public class HW1302 {
    static class ListNode{
        int value;
        public ListNode next;
        public ListNode(int value){
            this.value=value;
        }
        public ListNode(){
        }
    }

    public static void main(String[] args) {
        ListNode head = new ListNode();
        ListNode temp=head;
        for (int i=0;i<8;i++){

            temp.next=new ListNode(i);
            temp=temp.next;
        }

        ListNode heads= reverses(head);
        ListNode h=heads;
        for (int i=0;i<8;i++){
            System.out.println(h.value);
            h =h.next;

        }
      //  ListNode listNode=   getIndex(head,3);
        //System.out.println(listNode.value);
    }

    private static ListNode reverses(ListNode head) {
        ListNode pre = head;
        ListNode cur = head.next;
        pre.next  =null;
        while (cur!=null){
            ListNode newNext = cur.next;
           cur.next=pre;
           pre = cur;
           cur=newNext;
        }
        return pre;
    }

    private static ListNode getIndex(ListNode head, int k) {
        ListNode fast= head;
        ListNode slow=head;
        int i=0;
        while (i<k){
            //先进先出 把null 赋值给 头结点
            fast = fast.next;
            i++;
        }
        while (fast!=null){
            fast = fast.next;
            slow =slow.next;
        }
        return slow;
    }
}
